#include<stdio.h>
#include<stdint.h>
#include <conio.h>
#include <sys/stat.h>
#include<stdlib.h>
#include<windows.h>
#include"riscv_cpu_handle.h"
#include<sccb.h>
#include<stdio.h>



void ParameterInit(HANDLE *hComm,unsigned int argc,char*argv[])
{
    unsigned int i;
    char *s_temp;
    char buf[100];
    HANDLE handle;
    for(i = 0; i < argc; i++)
    {
        if(NULL != (s_temp = strstr(argv[i],"-com")))
        {
            sprintf((char*)buf,"\\\\.\\%s",s_temp + strlen("-com"));
            handle = CreateFile((char*)buf,GENERIC_READ|GENERIC_WRITE,
	            0, NULL, OPEN_EXISTING, 0, 0);
            if(handle != NULL)
            {
                *hComm = handle;
                break;
            }
        }
    }
}
void jpg_save(HANDLE hCom,unsigned int len)
{
    unsigned int i=0;
    FILE *fp;
    unsigned int rdata;
    fp = fopen("1.jpg","wb");
    for(i = 0; i < len; i+=4)
    {
        CPUmemoryReadWord(hCom,0x01000000+i,&rdata);
        fwrite(&rdata,4,1,fp);
        Sleep(1);
        printf("\r%dread",i);
    }
    fclose(fp);
    printf("\r\nlast data 0x%08x\r\n",rdata);
    printf("\r\n%d write ok\r\n",i);

    CPUmemoryReadWord(hCom,0x02000028,&rdata);
    if(rdata != i)
    {
        printf("\r\ntest_data:%d\r\n",rdata);
    }
}

void jpg_save2(HANDLE hCom,unsigned int len)
{
    unsigned int i=0;
    unsigned char*pbuf;
    FILE *fp;
    unsigned int rdata;
    pbuf = (unsigned char*)malloc(len);
    fp = fopen("1.jpg","wb");
    for(i = 0; i < len; i++)
    {
        CPUmemoryWriteWord(hCom,0x02000024,i);
        CPUmemoryReadWord(hCom,0x02000024,&rdata);
        if(rdata != i)
        {
            printf("\r\nerror i:%d rdata:%d\r\n",i,rdata);
        }
        CPUmemoryReadWord(hCom,0x02000028,&rdata);
        pbuf[i] = rdata&0xff;
        
        printf("\r%dread",i);
    }
    fwrite(pbuf,len,1,fp);
    Sleep(1);
    fclose(fp);
    printf("\r\nlast data 0x%08x\r\n",rdata);
    printf("\r\n%d write ok\r\n",i);
}

void ov2640_handle(HANDLE hCom)
{
    unsigned int rdata=0,len;
    jpg_init(hCom);
    CPUmemoryReadWord(hCom,0x02000020,&rdata);
    printf("\r%d byte",rdata);
    CPUmemoryWriteWord(hCom,0x0200001c,1);
    do
    {
        CPUmemoryReadWord(hCom,0x02000020,&rdata);
        printf("\r%d byte",rdata);
        CPUmemoryReadWord(hCom,0x0200001c,&rdata);
    } while ((rdata &1) != 0);
    printf("\r\njpg cap finish\r\n");
    CPUmemoryReadWord(hCom,0x02000020,&rdata);
    printf("%d byte\r\n",rdata);
    len = rdata;
    CPUmemoryReadWord(hCom,0x01000000,&rdata);
    printf("data 0x%08x\r\n",rdata);
    jpg_save(hCom,len);
}
void sram_mem_test(HANDLE hCom)
{
    unsigned int addr=0x01000000,rdata;
    unsigned int i;
    for(i = 0; i < 4096*10; i++)
    {
        CPUmemoryWriteWord(hCom,addr+i*4,0);
    }
    for(i = 0; i < 4096*10; i++)
    {
        CPUmemoryReadWord(hCom,addr+i*4,&rdata);
        if(rdata != 0)
        {
            printf("addr 0x%08x i:%d error %d\r\n",addr+i,i,rdata);
        }
    }
    puts("test finish\r\n");
}
void main(unsigned int argc,char*argv[])
{
    HANDLE hCom=NULL;
    ParameterInit(&hCom,argc,argv);
    if(hCom != NULL)
    {
        // sram_mem_test(hCom);
        ov2640_handle(hCom);
    }
    else
    {
        puts("open serial faild");
    }
}